package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            //是套餐
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            //不是套餐
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem item = serviceItemMapper.selectByPrimaryKey(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(item.getSaleStatus())) {
            //上架
            throw new BusinessException("上架服务项目不能修改，请下架后再修改");
        }
        if(ServiceItem.AUDITSTATUS_AUDITING.equals(item.getAuditStatus())){
            //审核中
            throw new BusinessException("服务项目正在审核中,不可修改");
        }
        serviceItemMapper.updateServiceItem(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    // 上架
    @Override
    public void saleOn(Long id) {
        ServiceItem item = serviceItemMapper.selectByPrimaryKey(id);
        if(item == null){
            throw new BusinessException("参数异常");
        }
        if(ServiceItem.CARPACKAGE_NO.equals(item.getCarPackage())){
            //不是套餐, 直接上架
            serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
            return;
        }
        if(ServiceItem.AUDITSTATUS_APPROVED.equals(item.getAuditStatus())){
            //审核通过
            serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
            return;
        }
        throw new BusinessException("只有在审核通过情况下才允许上架");
    }

    //下架
    @Override
    public void saleOff(Long id) {
        ServiceItem item = serviceItemMapper.selectByPrimaryKey(id);
        if(item == null){
            throw new BusinessException("参数异常");
        }
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void startAudit(Long serviceItemId, Long bpmnInfoId, Long director, Long finance, String info) {
        //获取修养服务对象
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(serviceItemId);
        if (serviceItem == null || !ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("参数异常！");
        }
        // 修改服务状态为审核中
        serviceItemMapper.changeAuditStatus(serviceItemId,ServiceItem.AUDITSTATUS_AUDITING);

        //审核信息记录
        CarPackageAudit packageAudit = new CarPackageAudit();
        if(director == null){
            throw new BusinessException("必须指定第一审核人(店长)");
        }
        packageAudit.setServiceItemId(serviceItemId);
        packageAudit.setServiceItemInfo(serviceItem.getInfo());
        packageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        packageAudit.setCreator(ShiroUtils.getUser().getUserName());
        packageAudit.setAuditorId(director);
        packageAudit.setBpmnInfoId(bpmnInfoId);
        packageAudit.setInfo(info);
        packageAudit.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        packageAudit.setCreateTime(new Date());
        carPackageAuditService.save(packageAudit);

        // 启动流程实例
        Map<String ,Object> map = new HashMap<>();
        map.put("director", director.toString());
        if (finance!=null) {
            map.put("finance", finance.toString());
        }
        // acit7 是不能使用BigDecimal 进行大小判断
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        //启动流程
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        String businessKey = packageAudit.getId().toString();
        // 参数1：流程定义Id，参数2：业务key，参数3：流程参数
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(),businessKey,map);

        //更新记录实例id
        packageAudit.setInstanceId(instance.getId());
        carPackageAuditService.updateInstanceId(packageAudit);
    }

    @Override
    public void changeAuditStatus(Long serviceItemId, Integer auditstatus) {
        serviceItemMapper.changeAuditStatus(serviceItemId,auditstatus);
    }
}
